package vn.wallet.app.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import vn.wallet.app.dao.IDBHelperDAO;

@Repository
public class DBHelperDAO implements IDBHelperDAO {

	@Resource
	private SessionFactory sessionFactory;
	
	
	//---------------------- HQL
	public Query createQuery(String hql){
		return getSessionFactory().getCurrentSession().createQuery(hql);
	}
	
	public Query createQuery(String hql, Object... params){
		Query query = createQuery(hql);
		for(int i = 0; i <= params.length; i++){
			query.setParameter(i, params[i]);
		}
		return query;
	}
	
	@Override
	public List<?> executeQuery(String hql) {
		return createQuery(hql).list();
	}

	@Override
	public List<?> executeQuery(String hql, Object... params) {
		return createQuery(hql, params).list();
	}
	
	@Override
	public int executeUpdate(String hql) {
		return createQuery(hql).executeUpdate();
		
	}
	
	@Override
	public int executeUpdate(String hql, Object... params) {
		return createQuery(hql, params).executeUpdate();
	}
	
	

	
	//---------------------- SQL
	public Query createSQLQuery(String sql){
		return getSessionFactory().getCurrentSession().createSQLQuery(sql);
	}
	
	public Query createSQLQuery(String sql, Object... params){
		Query query = createSQLQuery(sql);
		for(int i = 0; i <= params.length; i++){
			query.setParameter(i, params[i]);
		}
		return query;
	}
	
	@Override
	public List<?> executeSqlQuery(String sql) {
		return createSQLQuery(sql).list();
	}
	
	@Override
	public List<?> executeSqlQuery(String sql, Object... params) {
		return createSQLQuery(sql, params).list();
	}

	@Override
	public int executeSQLUpdate(String hql) {
		return createSQLQuery(hql).executeUpdate();
	}
	
	@Override
	public int executeSQLUpdate(String sql, Object... params) {
		return createSQLQuery(sql, params).executeUpdate();
	}
	
	
	
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
}
